package com.bill.gaty.mfidelity.util;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;
import android.util.Log;

public class Security {
public static byte[] genAESKey(int length){
	try {byte[] keyStart = (System.currentTimeMillis()+"GTF MFIDELITY+BILL-GATY").getBytes();
	KeyGenerator kgen;

		kgen = KeyGenerator.getInstance("AES");
	
	SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
	sr.setSeed(keyStart);
	kgen.init(length, sr);  
	SecretKey skey = kgen.generateKey();
	return  skey.getEncoded();    
	} catch (NoSuchAlgorithmException e) {
		// TODO Auto-generated catch block
		Log.e("NoSuchAlgorithmException", e.getMessage());
		 
	}
 catch ( Exception e) {
	// TODO Auto-generated catch block
	Log.e("genAESKey_Exception", e.getMessage());
	 
}
	return new byte[]{};
	
}
public static String genAESKeyBase64(int length){
	byte[] key=Security.genAESKey(length);
	return Base64.encodeToString(key, Base64.DEFAULT);
	 
}
public static byte[] decodeAESKeyBase64(String key){
	 
	return Base64.decode(key, Base64.DEFAULT);
	 
}
public static String encryptAES(byte key[],byte[] data){
	  try {SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  
		byte[] res = cipher.doFinal(data);
		return Base64.encodeToString(res, Base64.DEFAULT);
	} catch (IllegalBlockSizeException e) {
		// TODO Auto-generated catch block
		Log.e("IllegalBlockSizeException", e.getMessage());
	} catch (BadPaddingException e) {
		// TODO Auto-generated catch block
		Log.e("IllegalBlockSizeException", e.getMessage());
	} catch (NoSuchAlgorithmException e) {
		// TODO Auto-generated catch block
		Log.e("NoSuchAlgorithmException", e.getMessage());
	} catch (NoSuchPaddingException e) {
		// TODO Auto-generated catch block
		Log.e("NoSuchPaddingException", e.getMessage());
	} catch (InvalidKeyException e) {
		// TODO Auto-generated catch block
		Log.e("InvalidKeyException", e.getMessage());
	}
	  return Base64.encodeToString(data, Base64.DEFAULT);
}
public static String decryptAESNonBase64(byte key[],String data){
	String res=decryptAES(key,Base64.decode(data, Base64.DEFAULT));
	Log.i("decryptAESNonBase64-1", res);
	res=new String(Base64.decode(res, Base64.DEFAULT));
	Log.i("decryptAESNonBase64-2", res);
	return res.trim();
}
public static String decryptAES(byte key[],byte[] data){
	 try {SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
	    Cipher cipher = Cipher.getInstance("AES");
	    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
	  
			byte[] res = cipher.doFinal(data);
			String re= Base64.encodeToString(res, Base64.DEFAULT);
			Log.i("decryptAES-Base64.encodeToString", re);
			return re;
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			Log.e("IllegalBlockSizeException", e.getMessage());
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			Log.e("IllegalBlockSizeException", e.getMessage());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			Log.e("NoSuchAlgorithmException", e.getMessage());
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			Log.e("NoSuchPaddingException", e.getMessage());
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			Log.e("InvalidKeyException", e.getMessage());
		}
		  return Base64.encodeToString(data, Base64.DEFAULT);
	
}
}
